//
//  StarBar.m
//  ybzj
//
//  Created by liyuan on 15/7/7.
//  Copyright (c) 2015年 liyuan. All rights reserved.
//

#import "StarBar.h"


@implementation StarBar
@synthesize level;



// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
    CGFloat radius = self.frame.size.height/2;
    CGFloat th=1/180.0* M_PI;
    NSLog(@"sdssd");
    NSLog(@"%f",self.frame.size.height);
  
     /*计算小圆半径r0 */
    CGFloat r0 = radius * sin(18*th)/cos(36*th);
    CGFloat x1[5]={0},y1[5]={0},x2[5]={0},y2[5]={0};
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGMutablePathRef startPath = CGPathCreateMutable();
    
    for(int num=0;num<5;num++){
    
    CGFloat centerX = self.frame.size.width*(num*3+1)/14;
    CGFloat centerY = self.frame.size.height/2;
    
   for (int i = 0; i < 5; i ++)
    {
        x1[i] = centerX + radius * cos((90 + i * 72)*th); /* 计算出大圆上的五个平均分布点的坐标*/
        y1[i]=centerY - radius * sin((90 + i * 72)*th);
        x2[i]=centerX + r0 * cos((54 + i * 72)*th); /* 计算出小圆上的五个平均分布点的坐标*/
        y2[i]=centerY - r0 * sin((54 + i * 72)*th);
    }
    
    
    CGPathMoveToPoint(startPath, NULL, x1[0], y1[0]);
    
    
    for (int i = 1; i < 5; i ++) {
        CGPathAddLineToPoint(startPath, NULL, x2[i], y2[i]);
        CGPathAddLineToPoint(startPath, NULL, x1[i], y1[i]);
    }
    
    CGPathAddLineToPoint(startPath, NULL, x2[0], y2[0]);
    CGPathAddLineToPoint(startPath, NULL, x1[0], y1[0]);
    }
    CGContextAddPath(context, startPath);
    
    
    //CGContextSetStrokeColorWithColor(context, [[UIColor blackColor] CGColor]);
    //CGContextStrokePath(context);
           CGContextClip(context);
    for(int i=0 ;i<self.level;i++){
        CGContextSetFillColorWithColor(context, [[UIColor colorWithRed:247/255.0 green:176/255.0 blue:43/255.0 alpha:1] CGColor]);
       
        CGRect range = CGRectMake(self.frame.size.width*(i*3+1)/14+radius * cos((162)*th), 0, (x1[4] - x1[1]) , y1[2]);
        CGContextFillRect(context, range);
    }
    
    for(int i=self.level ;i<5;i++){
        CGContextSetFillColorWithColor(context, [[UIColor colorWithRed:161/255.0 green:163/255.0 blue:165/255.0 alpha:1] CGColor]);
        
        CGRect range = CGRectMake(self.frame.size.width*(i*3+1)/14+radius * cos((162)*th), 0, (x1[4] - x1[1]) , y1[2]);
        CGContextFillRect(context, range);
    }

    

    CFRelease(startPath);
}

@end
